// pages/my/order-detail/order-detail.js
const controllers = require('../../controllers/controllerOrder').controller
const app = getApp()

function InitTime(endtime) {
  var dd, hh, mm, ss = null;
  var time = (new Date(endtime.replace(/-/g, '/')).getTime() - new Date().getTime()) / 1000;
  if (time <= 0) {
    return '结束'
  } else {
    dd = Math.floor(time / 60 / 60 / 24);
    hh = Math.floor((time / 60 / 60) % 24);
    mm = Math.floor((time / 60) % 60);
    ss = Math.floor(time % 60);
    var str = dd + "天" + hh + ":" + mm + ":" + ss;
    return str;
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordNo: 0,
    state: 0,
    goodsList: [],
    imgurl: '',
    account: {},
    shopAccount: {},
    createTime: '',
    payType: '',
    desc: '',
    money: '',
    orderComment: {},
    marketAccountList: [],
    detail: {},
    downtime: '',
    timer: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //预览图片
  topre(e) {
    wx.previewImage({
      urls: [e.currentTarget.id],
    })
  },
  //完成服务
  completeService(e) {
    controllers.serviceComplete({
      orderNo: e.currentTarget.id,
      shopId: wx.getStorageSync('shopId'),
      shopAccountId: e.currentTarget.dataset.shopaccountid || '',
      type: 0,
    }).then(res => {
      wx.showToast({
        title: '成功',
        icon: 'none'
      })
      this.setData({
        pageNumber: 1,
        pageSize: 10,
        isEnd: false,
        orderList: []

      })
      //获取订单详情
      controllers.serviceorderDetail({
        shopId: wx.getStorageSync('shopId'),
        orderNo: this.data.ordNo,
        type: 0
      }).then(res => {
        console.log(res);
        this.setData({
          goodsList: res.data.goodsList,
          account: res.data.account,
          shopAccount: res.data.shopAccount,
          marketAccountList: res.data.marketAccountList,
          createTime: res.data.createTime,
          payType: res.data.payType,
          desc: res.data.desc,
          money: res.data.money,
          orderComment: res.data.orderComment,
          detail: res.data
        })
        if (this.data.detail.marketAccountList && this.data.detail.marketAccountList[0].endTime) {
          this.setData({
            timer: setInterval(() => {
              this.data.downtime = InitTime(this.data.detail.marketAccountList[0].endTime)
              this.setData({
                downtime: this.data.downtime
              })
            }, 1000)
          })
        }
        if (res.data.payType == 0) {
          this.setData({
            payType: '支付宝'
          })
        } else if (res.data.payType == 1) {
          this.setData({
            payType: '微信'
          })
        } else if (res.data.payType == 2) {
          this.setData({
            payType: '余额'
          })
        } else if (res.data.payType == 3) {
          this.setData({
            payType: '积分'
          })
        } else {
          this.setData({
            payType: '现金'
          })
        }
      })
    })
  },
  onLoad: function (options) {
    this.setData({
      imgurl: app.globalData.IMG_URL
    })
    if(!wx.getStorageSync('shopId')||!wx.getStorageSync('token')){
      wx.redirectTo({
        url: '/pages/login/index',
      })
    }else{
      this.setData({
        ordNo: options.id,
        state: options.state
      })
    }
    //获取订单详情
    controllers.serviceorderDetail({
      shopId: wx.getStorageSync('shopId'),
      orderNo: this.data.ordNo,
      type: 0
    }).then(res => {
      console.log(res);
      this.setData({
        goodsList: res.data.goodsList,
        account: res.data.account,
        shopAccount: res.data.shopAccount,
        marketAccountList: res.data.marketAccountList,
        createTime: res.data.createTime,
        payType: res.data.payType,
        desc: res.data.desc,
        money: res.data.money,
        orderComment: res.data.orderComment,
        detail: res.data
      })
      if (this.data.detail.marketAccountList && this.data.detail.marketAccountList[0].endTime) {
        this.setData({
          timer: setInterval(() => {
            this.data.downtime = InitTime(this.data.detail.marketAccountList[0].endTime)
            this.setData({
              downtime: this.data.downtime
            })
          }, 1000)
        })
      }
      if (res.data.payType == 0) {
        this.setData({
          payType: '支付宝'
        })
      } else if (res.data.payType == 1) {
        this.setData({
          payType: '微信'
        })
      } else if (res.data.payType == 2) {
        this.setData({
          payType: '余额'
        })
      } else if (res.data.payType == 3) {
        this.setData({
          payType: '积分'
        })
      } else {
        this.setData({
          payType: '现金'
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})